package com.share.ai.mcp.server.sse.service;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.util.Date;

/**
 * @Description 函数调用 (Function Calling)  通常与 FunctionCallback 配合
 * @Tool 工具调用 (Tool Calling)  直接标注在 Bean 的方法上
 * <p>
 * <p>
 * mcp 则使用 优先使用 @Tool 和 @ToolParam
 */
@Slf4j
@Service
public class OrderService {

    @Tool(name = "getOrderDetail", description = "获取订单详情信息")
    public Flux<String> getOrderDetail(@ToolParam(description = "订单号") String orderId) {
        log.info("获取订单详情信息,orderId={}", orderId);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("detail", "已经发货了");
        jsonObject.put("time", new Date().getTime());
        jsonObject.put("orderId", orderId);
        Flux<String> flux = Flux.create(sink -> {
            try {
                sink.next(jsonObject.toString());
//                sink.complete(); // 必须调用 complete
            } catch (Exception e) {
                log.info("推送异常",e);
                sink.error(e); // 异常处理
            }
        });
        return flux;
    }
}
